package com.atguigu.gulimall.ware.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //   status: 0,//状态
        //   wareId: 1,//仓库id

        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");//模糊检索字段
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{//代表这两个关键字都可以查询
               wrapper.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }
        //这是专门查寻状态的
        String status = (String) params.get("status");//状态
        if(!StringUtils.isEmpty(status)){
                queryWrapper.eq("status",status);
        }
        //这是专门查询仓库的
        String wareId = (String) params.get("wareId");//仓库id
        if(!StringUtils.isEmpty(wareId)){
                queryWrapper.eq("ware_id",wareId);
        }
        //以上sql的意思为：Select * from wms_purchase_detail where status=1         and (purchase_id=1   or   sku_id=1)

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
              queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listByDetailId(Long id) {
        List<PurchaseDetailEntity> purchaseId = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));

        return purchaseId;
    }

}